<?php

	// +----------------------------------------------------------------------
	// | User的event操作
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\store;

	use app\event\store\Store as StoreEvent;
	use think\facade\Db;
	use app\common\event\Base;
	use app\model\ucenter\User as UserModel;
	use app\model\store\StoreAuth as StoreAuthModel;
	use app\event\ucenter\Notice as NoticeEvent;
	use think\Exception;

	class StoreAuth extends Base
	{

		/**
		 * 认证审核
		 * @param array $param
		 * @param UserModel $admin
		 * @return mixed
		 */
		public function examine($param, $admin)
		{
			try {
				if (!$admin) {
					throw new Exception('管理不存在');
				}
				$auth = StoreAuthModel::where('id', $param['id'])->where('status', 0)->find();
				if (!$auth) {
					throw new Exception('认证信息异常');
				}
				$auth['store_nickname'] = $param['store_nickname'];
				$auth['store_contacts'] = $param['store_contacts'];
				$auth['store_mobile'] = $param['store_mobile'];
//				$auth['front_id'] = $param['front_id'];
//				$auth['back_id'] = $param['back_id'];
//				$auth['hand_id'] = $param['hand_id'];
				$auth['idcard_name'] = $param['idcard_name'];
				$auth['idcard_no'] = $param['idcard_no'];
				$auth['result'] = $param['result'];
				// 发送消息
				$message = [];
				$message['type'] = 'auth';
				$message['relation_id'] = $auth['id'];
				$message['from_user_id'] = 10000;
				$user = $auth->user;
				// 开启事务
				Db::startTrans();
				if (1 == $param['examine']) {
					// 通过认证;
					$user['type'] = 2;
					$auth['status'] = 1;
					$auth['result'] = '开通店铺成功';
					$data['message'] = '开通店铺成功';
					// 初始化店铺信息
					$store = StoreEvent::initInfo($user, $auth);
					$user['store_id'] = $store['id'];
					if (!$user->save()) {
						throw new Exception('认证信息保存异常');
					}
					$auth['store_id'] =  $store['id'];
				} else {
					// 拒绝认证
					if (!$param['result']) {
						throw new Exception('请填写拒绝理由');
					}
					$auth['status'] = -1;
					$data['message'] = '开通认证被拒绝';
					$message['result'] = $param['result'];
				}
				if (!$auth->save()) {
					throw new Exception('认证保存异常');
				}
				//                $message['join_queue'] = 0;
				NoticeEvent::send($message, $auth->user);
				// 提交事务
				Db::commit();
				// 返回订单数据
				$data['code'] = 0;
			} catch (Exception $e) {
				// 回滚事务
				Db::rollback();
				$data['message'] = $e->getMessage();
				$data['line'] = $e->getLine();
				$data['code'] = 1;
			}
			// 发送卖家通知
			return $data;
		}


		/**
		 * 提交认证
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function add($param, $user = null)
		{
			try {
				if (!$user) {
					throw new Exception('用户不存在');
				}
				// 已经认证的不再提交
				if ($user['store_id']) {
					throw new Exception('已经入驻，无需提交');
				}
				// 如果有历史提交，则置为无效
				if ($user->storeAuths()->where('status', '>', -1)->find()) {
					$user->storeAuths()->where('status', '>', -1)->update(['status' => -2]);
				}
				$auth = $param;
				$auth['user_id'] = $user['id'];
				unset($auth['id']);
				// 完善信息
				$auth = $user->storeAuths()->save($auth);
				if (!$auth) {
					throw new Exception('店铺认证提交异常');
				}
				$data['message'] = '入驻认证已提交，请等待审核';
				// 返回订单数据
				$data['code'] = 0;
			} catch (Exception $e) {
				$data['message'] = $e->getMessage();
				$data['code'] = 1;
			}
			return $data;
		}

	}
